<?php

/**
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: goods.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
if ($_REQUEST['id']=='1658') {
    header("location: glair.php?isInfo=1");
}

if(!empty($_REQUEST['act']) && $_REQUEST['act'] == 'click' && !empty($_REQUEST['id']) && $id = intval($_REQUEST['id'])){
    $db->query('UPDATE ' . $ecs->table('goods') . " SET click_count = click_count + 1 WHERE goods_id = '$id'");
    $row = $db->getRow('SELECT click_count FROM ' . $ecs->table('goods') . " WHERE goods_id = '$id'");
    echo $row['click_count'];
    exit;
}

assign_template();
$goods_id = !empty($_GET['id']) ? intval($_GET['id']) : '';
$area_id = !empty($_SESSION['area_id'])?intval($_SESSION['area_id']) : '3';

$act = !empty($_GET['act']) ? $_GET['act'] : '';
$smarty->assign('goods_id', $goods_id);
$smarty->assign('top_title', $_LANG['goods_note']);
//获取商品信息
$goods_info = get_goods_info($goods_id);

if ($goods_info === false || !$goods_info['is_on_sale'])
{
   /* 如果没有找到任何记录则跳回到首页 */
        ecs_header("Location: http://www.eggms.com/errpage/goods.php\n");
   exit;
}
if($goods_info['promote_end_date']<time()){
    $goods_info['promote_price']='';
}

$goods_info['comment_rank'] = $goods_info['comment_rank']*20;


//商品促销中处理
if($goods_info['promote_end_date']>time() && $goods_info['promote_start_date']<time()){
    $goods_info['goods_number']=limit_num($goods_id);
    $smarty->assign('end_time',        ($goods_info['promote_end_date']-time())); 
    $smarty->assign('end_time_now',    promote_time_out($goods_info['promote_end_date']-time())); 
}

$smarty->assign('goods_info', $goods_info);
for ($i=1; $i <= $goods_info['goods_number']; $i++) { 
    $stock[$i]=$i;
}
$smarty->assign('stock', $stock);

$shop_price   = $goods_info['shop_price'];
// 获取商品所有图片
$sql = 'SELECT img_id, img_desc, thumb_url, img_url'.
       ' FROM ' .$ecs->table('goods_gallery').
       " WHERE goods_id = '$goods_id' ORDER BY img_id";
$img_list = $db->getAll($sql);
foreach ($img_list AS $key => $img)
{
    $gallery["pic_".$key] = get_image_path($_REQUEST['id'], $img_list[$key]['img_url'], false, 'gallery');
}
$smarty->assign('pic',  $gallery);

$properties = get_goods_properties($goods_id);  // 获得商品的规格和属性
$smarty->assign('specification',       $properties['spe']);  // 商品规格
foreach ($properties['spe'] as $key => $value) {
    if($value['name']=='颜色'){
        $color=$value['values'];
    }
}
if (!empty($color)) {
    $smarty->assign('color',       $color);  // 商品颜色
}
$smarty->assign('cfg',          $_CFG);
$cmt = assign_comment($goods_id,0);
$smarty->assign('id',           $goods_id);
$smarty->assign('type',         0);
$smarty->assign('comments',     $cmt['comments']);
$smarty->assign('pager',        $cmt['pager']);
$smarty->assign('comment', $comment);//评论
$smarty->assign('goods_info', $goods_info);
$linked_goods = get_linked_goods($goods_id);// 关联商品
$smarty->assign('related_goods',       $linked_goods);

if (empty($goods_info['goods_desc']) && !empty($goods_info['get_url'])) {
    header("location: {$goods_info['get_url']}");
    exit;
}
if (!empty($goods_info['is_taodan'])) {
    $smarty->display('goods_taodan.dwt');
} else {
    
    //去拿用户信息
    $user_array = array();
    $sql = 'SELECT * FROM ' . $ecs->table('users') . ' WHERE user_id = '.$_SESSION['user_id'];
    $user_array = $db->getRow($sql);
    
    $smarty->assign('user_array', $user_array);
    
    $smarty->display('goods.dwt');
}

/**
 * 获得指定商品的各会员等级对应的价格
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_user_rank_prices($goods_id, $shop_price)
{
    $sql = "SELECT rank_id, IFNULL(mp.user_price, r.discount * $shop_price / 100) AS price, r.rank_name, r.discount " .
            'FROM ' . $GLOBALS['ecs']->table('user_rank') . ' AS r ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                "ON mp.goods_id = '$goods_id' AND mp.user_rank = r.rank_id " .
            "WHERE r.show_price = 1 OR r.rank_id = '$_SESSION[user_rank]'";
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {

        $arr[$row['rank_id']] = array(
                        'rank_name' => htmlspecialchars($row['rank_name']),
                        'price'     => price_format($row['price']));
    }

    return $arr;
}
/**
 * 获得指定商品的关联商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_linked_goods($goods_id)
{
    $sql = 'SELECT g.goods_id,g.goods_name, g.goods_brief ' .
            'FROM ' . $GLOBALS['ecs']->table('link_goods') . ' lg ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON (g.goods_id = lg.link_goods_id OR g.goods_id =lg.goods_id)' .
            "WHERE lg.goods_id = '$goods_id' AND g.is_alone_sale = 1 AND g.is_delete = 0  AND g.is_on_sale = 1 ".
            "GROUP BY g.goods_id";

    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']     = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']   = $row['goods_name'];
        $arr[$row['goods_id']]['goods_brief']  = end(explode('-',$row['goods_name']));
        $arr[$row['goods_id']]['url']          = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
    }

    return $arr;
}


?>